﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PMS.App
{
    public partial class ucEmployee : UserControl
    {
        Status status = Status.Update;
        public ucEmployee()
        {
            InitializeComponent();
        }

        private void ucEmployee_Load(object sender, EventArgs e)
        {
            this.nhanVienTableAdapter.Fill(dsPMS.NhanVien);
            this.phongBanTableAdapter.Fill(dsPMS.PhongBan);
            this.chucVuTableAdapter.Fill(dsPMS.ChucVu);
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            if (status == Status.Update)
            {
                if (XtraMessageBox.Show("Do you want to save changes?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    this.nhanVienTableAdapter.Update(this.dsPMS.NhanVien);
                }
                else
                {
                    this.nhanVienTableAdapter.Fill(this.dsPMS.NhanVien);
                }
            }
            if (status == Status.New)
            {
                this.nhanVienTableAdapter.Update(this.dsPMS.NhanVien);
                status = Status.Update;
            }
        }

        private void datnaviMain_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.EndEdit:
                    if (XtraMessageBox.Show("Do you want to save changes?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        this.nhanVienTableAdapter.Update(this.dsPMS.NhanVien);
                    }
                    break;
                case NavigatorButtonType.CancelEdit:
                    if (XtraMessageBox.Show("Are you sure you want to delete a Employee?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        this.nhanVienBindingSource.Remove(this.nhanVienBindingSource.Current);
                        this.nhanVienTableAdapter.Update(this.dsPMS.NhanVien);
                    }
                    break;
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (XtraMessageBox.Show("Are you sure you want to delete a Employee?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    this.nhanVienBindingSource.Remove(this.nhanVienBindingSource.Current);
                    this.nhanVienTableAdapter.Update(this.dsPMS.NhanVien);
                }
            }
        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            status = Status.New;
        }

    }
}
